<template>
  <div id="userEditPasswordPage">
    <div class="edit-password-container">
      <div class="edit-password-header">
        <h2 class="title">修改密码</h2>
        <div class="desc">图片协作平台</div>
      </div>

      <a-form
        :model="formState"
        name="basic"
        autocomplete="off"
        @finish="handleSubmit"
        layout="vertical"
        class="edit-password-form"
      >
        <a-form-item
          label="请输入原密码"
          name="userOldPassword"
          :rules="[{ required: true, message: '请输入原密码' }]"
        >
          <a-input-password
            v-model:value="formState.userOldPassword"
            placeholder="请输入原密码"
            size="large"
          />
        </a-form-item>

        <a-form-item
          label="新密码"
          name="userNewPassword"
          :rules="[
            { required: true, message: '请输入新密码' },
            { min: 8, message: '密码不能小于 8 位' },
          ]"
        >
          <a-input-password
            v-model:value="formState.userNewPassword"
            placeholder="请输入新密码"
            size="large"
          />
        </a-form-item>

        <a-form-item
          label="确认密码"
          name="userCheckPassword"
          :rules="[
            { required: true, message: '请输入确认密码' },
            { min: 8, message: '确认密码不能小于 8 位' },
          ]"
        >
          <a-input-password
            v-model:value="formState.userCheckPassword"
            placeholder="请输入确认密码"
            size="large"
          />
        </a-form-item>

        <a-form-item>
          <a-button
            type="primary"
            html-type="submit"
            style="width: 100%"
            size="large"
            :loading="loading"
          >
            修改
          </a-button>
        </a-form-item>
      </a-form>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { updateMyUserUsingPost } from '@/api/userController.ts'
import { message } from 'ant-design-vue'
import { useRouter } from 'vue-router'

// 表单状态
const formState = reactive<API.UserUpdateMyRequest>({
  userOldPassword: '',
  userNewPassword: '',
  userCheckPassword: '',
})

// 修改密码加载状态
const loading = ref(false)

// 路由
const router = useRouter()

/**
 * 表单提交
 * @param values
 */
const handleSubmit = async (values: any) => {
  // 判断两次输入的密码是否一致
  if (formState.userNewPassword !== formState.userCheckPassword) {
    message.error('二次输入的密码不一致')
    return
  }

  loading.value = true
  try {
    const res = await updateMyUserUsingPost(values)

    if (res.data.code === 0 && res.data.data) {
      message.success('修改成功')
      router.push({
        path: '/user/login',
        replace: true,
      })
    } else {
      message.error(`修改失败，${res.data.message || '未知错误'}`)
    }
  } catch (e) {
    message.error(`修改失败，${e.message || '网络错误'}`)
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
#userEditPasswordPage {
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding-top: 60px; /* 调整顶部内边距 */
  overflow: auto;
}

.edit-password-container {
  width: 100%;
  max-width: 360px;
  background-size: cover;
  background-position: center;
  background-attachment: fixed;
  background-color: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(10px);
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
  padding: 32px;
  margin-top: 20px; /* 调整顶部外边距 */
  transition: all 0.3s ease;
  z-index: 100; /* 确保输入框在最上层 */
}

.edit-password-container:hover {
  transform: translateY(-5px);
  box-shadow: 0 15px 40px rgba(0, 0, 0, 0.3);
}

.edit-password-header {
  text-align: center;
  margin-bottom: 24px;
}

.title {
  font-size: 28px;
  font-weight: 600;
  color: #333;
  margin-bottom: 8px;
}

.desc {
  color: #888;
  font-size: 14px;
}

.edit-password-form {
  margin-top: 24px;
}

:deep(.ant-btn-primary) {
  background-color: #1890ff;
  border-color: #1890ff;
  font-weight: 500;
  border-radius: 8px;
  padding: 12px;
  transition: all 0.3s;
}

:deep(.ant-btn-primary:hover) {
  background-color: #40a9ff;
  border-color: #40a9ff;
}

:deep(.ant-btn-primary:active) {
  background-color: #096dd9;
  border-color: #096dd9;
}

:deep(.ant-input) {
  border-radius: 8px;
  padding: 12px;
  font-size: 15px;
  transition: all 0.3s;
}

:deep(.ant-input:focus) {
  border-color: #1890ff;
  box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.2);
}

:deep(.ant-form-item-label) {
  font-weight: 500;
  color: #333;
}

:deep(.ant-form-item-explain) {
  margin-top: 8px;
  font-size: 13px;
}

@media (max-width: 600px) {
  .edit-password-container {
    padding: 24px;
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.15);
  }

  .title {
    font-size: 24px;
  }
}
</style>
